package dql.com.ordsystem;

import java.util.ArrayList;
import java.util.Iterator;


//import dql.com.ordsystem.Lunch.MyAction;
import dql.com.ordsystem.OrderItemAdapt.ViewHolder;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Dinner extends Activity {

	private ListView lv;
	private ArrayList<String> order=new ArrayList<String>(); // ordered Items from table
	private ArrayList<String> price=new ArrayList<String>();
	private Table table; // global variant of table
	private OrderItemAdapt oia; // the adapt of the ListView
	private LayoutInflater inflater;
	private LinearLayout lin;
	private ArrayList<String> softdrinks;
	private ArrayList<String> saftdrinks;
	private ArrayList<String> waterdrinks;
	private ArrayList<String> beerdrinks;
	private ArrayList<String> hotdrinks;
	private ArrayList<String> salats;
	private ArrayList<String> bakes;
	private ArrayList<String> fries;
	private ArrayList<String> sushis;
	private ArrayList<String> sake;
	private ArrayList<String> wein;
	private ArrayList<String> drinkslist=new ArrayList<String>();
	private Item it;
	
	@Override
	protected void onStart() {
	    super.onStart();  // Always call the superclass method first
	    table = (Table) getApplicationContext();
	    if(table.getNum().equals(""))
	    {
	    	Intent intent =new Intent(this, MainActivity.class);
			startActivity(intent);
	    }
	    
	
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dinner);
		inflater = getLayoutInflater();
		lin = (LinearLayout) findViewById(R.id.setlunchview);
		table = (Table) getApplicationContext();
		it = new Item(getApplicationContext());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_dinner, menu);
		return true;
	}

	public void onDelete(View view) {

		for (int i = 0; i < lv.getCount(); i++) {
			if (OrderItemAdapt.getIsSelected().get(i)) {
				Log.i("------", Integer.toString(i));
				String s = OrderItemAdapt.getlist().get(i);
				String s2 = OrderItemAdapt.getlist2().get(i);
				Log.i("xxxxxxxx", s);
				order.remove(s);
				price.remove(s2);
			}
		}
		Log.i("yyyyyyyyyy", Integer.toString(order.size()));
		oia = new OrderItemAdapt(order, price,this);
		lv.setAdapter(oia);
		Toast.makeText(getApplicationContext(),
				"you have " + table.getOrder().size() + " items in your car",
				Toast.LENGTH_SHORT).show();

	}
	
	public boolean isDinner(){
		
		Time t1 = new Time();
		Time t2=new Time();
		t1.setToNow();
		t2.setToNow();
		t2.hour=17;
		t2.minute=0;
		if(t1.after(t2)){
			return true;
		} else {
			return true;
		}
		
	}

	public void goDinner(View view) {

		if(this.isDinner()){
		
		ScrollView layout = (ScrollView) inflater.inflate(R.layout.dinner, null)
				.findViewById(R.id.dinnerview);
		
		lin.removeAllViews();
		lin.addView(layout);
		}else{
			Toast.makeText(getApplicationContext(), "not dinner time", Toast.LENGTH_LONG).show();
		}

	}
	
	public void goSoftDrink(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		softdrinks = it.getAllSoftDrink();
		drinkslist.clear();
		for(String s:softdrinks){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinkslist.add("1 Coffeinhaltig 2 mit Farbstoff 3 Suessungsmitteln 4 mit Antioxidotionsmittel 5 enthaelt eine Phenylalaninquelle");
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				if(position!=softdrinks.size()){
				table.addOrder(softdrinks.get(position));
				table.getPrices().add(it.getPrice(softdrinks.get(position)));
				Toast.makeText(getApplicationContext(),
						softdrinks.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}
				}

		});
	}
	
	public void goSaftDrink(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		saftdrinks = it.getAllSaftDrink();
		drinkslist.clear();
		for(String s:saftdrinks){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(saftdrinks.get(position));
				table.getPrices().add(it.getPrice(saftdrinks.get(position)));
				Toast.makeText(getApplicationContext(),
						saftdrinks.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void goWaterDrink(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		waterdrinks = it.getAllWaterDrink();
		drinkslist.clear();
		for(String s:waterdrinks){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(waterdrinks.get(position));
				table.getPrices().add(it.getPrice(waterdrinks.get(position)));
				Toast.makeText(getApplicationContext(),
						waterdrinks.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void goBeerDrink(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		beerdrinks = it.getAllBeerDrink();
		drinkslist.clear();
		for(String s:beerdrinks){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(beerdrinks.get(position));
				table.getPrices().add(it.getPrice(beerdrinks.get(position)));
				Toast.makeText(getApplicationContext(),
						beerdrinks.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void goHotDrink(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		hotdrinks = it.getAllHotDrink();
		drinkslist.clear();
		for(String s:hotdrinks){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(hotdrinks.get(position));
				table.getPrices().add(it.getPrice(hotdrinks.get(position)));
				Toast.makeText(getApplicationContext(),
						hotdrinks.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}

	public void goSalat(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		salats = it.getSalat();
		drinkslist.clear();
		for(String s:salats){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(salats.get(position));
				table.getPrices().add(it.getPrice(salats.get(position)));
				Toast.makeText(getApplicationContext(),
						salats.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	public void goBake(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		bakes = it.getBake();
		drinkslist.clear();
		for(String s:bakes){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(bakes.get(position));
				table.getPrices().add(it.getPrice(bakes.get(position)));
				Toast.makeText(getApplicationContext(),
						bakes.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void goFry(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		fries = it.getFry();
		drinkslist.clear();
		for(String s:fries){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(fries.get(position));
				table.getPrices().add(it.getPrice(fries.get(position)));
				Toast.makeText(getApplicationContext(),
						fries.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void goSushi(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		sushis = it.getSushi();
		drinkslist.clear();
		for(String s:sushis){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(sushis.get(position));
				table.getPrices().add(it.getPrice(sushis.get(position)));
				Toast.makeText(getApplicationContext(),
						sushis.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void oderDinner(View view) {
		String order;
		switch (view.getId()) {
		case R.id.dinnerb1:
			order = this.getString(R.string.dinner1);
			table.addOrder(order);
			table.getPrices().add(this.getString(R.string.dinner1price));
			Toast.makeText(getApplicationContext(),
					order + " is added to you car", Toast.LENGTH_SHORT).show();
			break;
		//case R.id.dinnerb2:
			//order = this.getString(R.string.dinner2);
			//table.addOrder(order);
			//table.getPrices().add(this.getString(R.string.dinner2price));
			//Toast.makeText(getApplicationContext(),
			//		order + " is added to you car", Toast.LENGTH_SHORT).show();
		//	break;
		//case R.id.dinnerb3:
		//	order = this.getString(R.string.dinner3);
		//	table.addOrder(order);
		//	table.getPrices().add(this.getString(R.string.dinner3price));
		//	Toast.makeText(getApplicationContext(),
		//			order + " is added to you car", Toast.LENGTH_SHORT).show();
		//	break;
		case R.id.dinnerb4:
			order = this.getString(R.string.dinner4);
			table.addOrder(order);
			table.getPrices().add(this.getString(R.string.dinner4price));
			Toast.makeText(getApplicationContext(),
					order + " is added to you car", Toast.LENGTH_SHORT).show();
			break;
		case R.id.dinnerb5:
			order = this.getString(R.string.dinner5);
			table.addOrder(order);
			table.getPrices().add(this.getString(R.string.dinner5price));
			Toast.makeText(getApplicationContext(),
					order + " is added to you car", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;

		}

	}
	public void goWein(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		wein = it.getWein();
		drinkslist.clear();
		for(String s:wein){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(wein.get(position));
				table.getPrices().add(it.getPrice(wein.get(position)));
				Toast.makeText(getApplicationContext(),
						wein.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}
	
	public void goSake(View View) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.activity_drink, null).findViewById(R.id.drink);
		lin.removeAllViews();
		lin.addView(layout);
		ListView drinklist = (ListView) findViewById(R.id.drinklist);
		sake = it.getSake();
		drinkslist.clear();
		for(String s:sake){
			drinkslist.add(s+"   "+it.getPrice(s));
		}
		drinklist.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, drinkslist));
		drinklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				table.addOrder(sake.get(position));
				table.getPrices().add(it.getPrice(sake.get(position)));
				Toast.makeText(getApplicationContext(),
						sake.get(position) + " is added to you car",
						Toast.LENGTH_SHORT).show();

			}

		});
	}

	public void goShopCar(View view) {

		RelativeLayout layout = (RelativeLayout) inflater.inflate(
				R.layout.activity_my_car, null).findViewById(R.id.MyCar);
		lin.removeAllViews();
		lin.addView(layout);
		order = table.getOrder();
		price=table.getPrices();
		lv = (ListView) findViewById(R.id.mycarlist);
		// Toast.makeText(getApplicationContext(),
		// "M1 is added to you car"+table.getOrder().size(),
		// Toast.LENGTH_SHORT).show();
		oia = new OrderItemAdapt(order,price, this);
		lv.setAdapter(oia);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				// get the ViewHolder Object avoid through the findViewById to
				// get view
				ViewHolder holder = (ViewHolder) arg1.getTag();
				// toggle the checkbox
				holder.cb.toggle();
				// record the checkbox
				OrderItemAdapt.getIsSelected().put(arg2, holder.cb.isChecked());
			}
		});

	}

	public void finishShopping(View view) {
		EditText code =(EditText)findViewById(R.id.codeinput);
		table.setCode(code.getText().toString());
		new MyAction().execute("add");

	}
	
	 public void payBill(View view) {
         new MyAction().execute("pay");

 }
		public void goService(View view) {
			
			new MyAction().execute("service");

		}

	 public void payBillCard(View view) {
			new MyAction().execute("paycard");

		}
		
		private class MyAction extends AsyncTask<String, Void, Boolean> {

			@Override
			protected Boolean doInBackground(String... params) {
				String url = getResources().getString(R.string.server);
				if (params[0].equals("add")) {

					String or = "";
					Log.e("---------", url);
					Iterator<String> it = order.iterator();
					while (it.hasNext()) {
						or = or + it.next() + ";";
						Log.e("---------", or);

					}

					return OrderManager.addOrder(url, table.getNum(), or,table.getCode());
				}
				if (params[0].equals("pay")) {
					Log.e("-------aaaa--", table.getNum());

					return OrderManager.payOrder(url, table.getNum());
				}
				
				if (params[0].equals("paycard")) {
					Log.e("-------aaaa--", table.getNum());

					return OrderManager.payOrderCard(url, table.getNum());
				}
				
				if (params[0].equals("service")) {
					Log.e("-------aaaa--", table.getNum());

					return OrderManager.service(url, table.getNum());
				}
				
				return false;

			}

			protected void onPostExecute(Boolean res) {
				if (res) {
					order.clear();
					price.clear();
					oia = new OrderItemAdapt(order, price,getApplicationContext());
					if(lv==null){
						Toast.makeText(getApplicationContext(), "please order first.",
								Toast.LENGTH_SHORT).show();
					}else {
					lv.setAdapter(oia);
					Toast.makeText(getApplicationContext(), "you order is sended",
							Toast.LENGTH_SHORT).show();
					}
					
				} else {
					Toast.makeText(getApplicationContext(), "Please try it again",
							Toast.LENGTH_SHORT).show();
				}

			}

		

 
	}

}
